//
//  HubuSecondViewController.m
//  iHubu2
//
//  Created by hanshui on 12-10-31.
//  Copyright (c) 2012年 hanshui. All rights reserved.
//

#import "HubuSecondViewController.h"
#import "HubuQueryViewController.h"
#import "HubuAppDelegate.h"

@interface HubuSecondViewController ()

@end

@implementation HubuSecondViewController
@synthesize bookSearchBar;
@synthesize keywordView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[[self.bookSearchBar subviews] objectAtIndex:0] removeFromSuperview];
    
    [self keywordShow];
}

- (void)keywordShow {
    NSArray *keywords = [[NSArray alloc] initWithObjects:@"计算机", @"文学", @"心理咨询",
                         @"公务员", @"政策过程", @"信访", @"ppt", @"人力资源",
                         @"cmos", @"家谱", nil];
    for(int i=0; i<[keywords count]; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(arc4random() % 250, arc4random() % 250, [[keywords objectAtIndex:i] length] * 24, 25)];
        [button setTitle:[keywords objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:((arc4random() % 99)/99.0) green:((arc4random() % 99)/99.0) blue:((arc4random() % 99)/99.0) alpha:1.0] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:(15 + arc4random() % 8)];
        [button addTarget:self action:@selector(keywordClicked:) forControlEvents:UIControlEventTouchUpInside];
        [keywordView addSubview:button];
        [button release];
    }
    [keywords release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)hiddenKeyboard:(id)sender {
    bookSearchBar.text = @"";
    [bookSearchBar resignFirstResponder];
}

-(IBAction)keywordClicked:(id)sender {
    UIButton *button = (UIButton *)sender;
    HubuQueryViewController *hubuQueryViewController = [[HubuQueryViewController alloc] initWithNibName:@"HubuQueryViewController" bundle:nil];
    [hubuQueryViewController showSearchListInfo:button.titleLabel.text];
    
    HubuAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:hubuQueryViewController animated:YES];
    [hubuQueryViewController release];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    HubuQueryViewController *hubuQueryViewController = [[HubuQueryViewController alloc] initWithNibName:@"HubuQueryViewController" bundle:nil];
    [hubuQueryViewController showSearchListInfo:searchBar.text];
    
    HubuAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navigationController pushViewController:hubuQueryViewController animated:YES];
    [hubuQueryViewController release];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text = @"";
    [searchBar resignFirstResponder];
}
@end
